package drds.binlog.binlog_event;

import drds.binlog.common.Authors;
import drds.common.Author;


@Author(name = Authors.LI_YANG)
public class LogPosition implements Cloneable, Comparable<LogPosition>
{

    protected String fileName;
    protected long position;

    /**
     * @param fileName file name for binlog files: mysql-bin.000001
     */
    public LogPosition(String fileName)
    {
        this.fileName = fileName;
        this.position = 0L;
    }

    /**
     * @param fileName file name for binlog files: mysql-bin.000001
     */
    public LogPosition(String fileName, final long position)
    {
        this.fileName = fileName;
        this.position = position;
    }

    public LogPosition(LogPosition logPosition)
    {
        this.fileName = logPosition.fileName;
        this.position = logPosition.position;
    }

    public final String getFileName()
    {
        return fileName;
    }

    public final long getPosition()
    {
        return position;
    }

    public LogPosition clone()
    {
        try
        {
            return (LogPosition) super.clone();
        } catch (CloneNotSupportedException e)
        {
            // Never happend
            return null;
        }
    }


    public int compareTo(LogPosition logPosition)
    {
        final int compareTo = fileName.compareTo(logPosition.fileName);

        if (compareTo == 0)
        {
            return (int) (position - logPosition.position);
        }
        return compareTo;
    }

    public boolean equals(Object object)
    {
        if (object instanceof LogPosition)
        {
            LogPosition logPosition = ((LogPosition) object);
            return fileName.equals(logPosition.fileName) && (this.position == logPosition.position);
        }
        return false;
    }

}
